package com.qjcc.service.impl;

import com.qjcc.domain.AjaxJson;
import com.qjcc.service.CallCenterService;
import com.qjcc.util.SQLServerDataSourceFactory;
import com.qjcc.util.SQLServerDataSourceFactoryCall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class CallCenterServiceImpl implements CallCenterService {

    private static final int SECONDS = 31;


    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate secondaryJdbc;

    @Override
    public AjaxJson call(){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        SQLServerDataSourceFactoryCall sdsf = SQLServerDataSourceFactoryCall.getInstance();
        List<Map<String , Object>> result = null;
        List<Map<String , Object>> resultDay = null;
        try {
            result = sdsf.getCallList();
            resultDay = sdsf.getCallListDay();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //按月统计begin
        Collections.reverse(result);

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: result){
            categories.add(date.get("call_date").toString());
        }

        //纵坐标
        List<Double> columnData = new ArrayList<>();
        List<Double> splineData = new ArrayList<>();
        for(Map<String, Object> obj: result){
            double ct = Double.parseDouble(obj.get("ct").toString());
            columnData.add(ct);
            splineData.add(ct);
        }

        m.put("categories", categories);
        m.put("columnData", columnData);
        m.put("splineData", splineData);
        //按月统计end

        //按本月天数统计begin
        //横坐标
        List<String> categoriesDay = new ArrayList<>();
        for(Map<String, Object> date: resultDay){
            categoriesDay.add(date.get("day").toString());
        }
        //纵坐标
        List<Double> columnDataDay = new ArrayList<>();
        List<Double> splineDataDay = new ArrayList<>();
        for(Map<String, Object> obj: resultDay){
            double ct = Double.parseDouble(obj.get("ct").toString());
            columnDataDay.add(ct);
            splineDataDay.add(ct);
        }
        m.put("categoriesDay", categoriesDay);
        m.put("columnDataDay", columnDataDay);
        m.put("splineDataDay", splineDataDay);
        //按本月天数统计end

        //服务器时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月");
        m.put("serverDateStr", sf.format(new Date()));

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson workcard(){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        StringBuilder sql = new StringBuilder();
        sql.append("select Y.* From ( ");
        sql.append(" select * from ( ");
        sql.append("  select count(*) as order_count, to_char(c.SEND_TIME,'yyyy-mm') as date_month from cc_order c group by to_char(c.SEND_TIME,'yyyy-mm') ");
        sql.append(" ) X  order by X.date_month desc ");
        sql.append(") Y where ROWNUM <= 6");

        List<Map<String, Object>> result = secondaryJdbc.queryForList(sql.toString());

        Collections.reverse(result);

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: result){
            categories.add(date.get("DATE_MONTH").toString());
        }

        //纵坐标
        List<Double> columnData = new ArrayList<>();
        List<Double> splineData = new ArrayList<>();
        for(Map<String, Object> obj: result){
            double orderCount = Double.parseDouble(obj.get("ORDER_COUNT").toString());
            columnData.add(orderCount);
            splineData.add(orderCount);
        }

        m.put("categories", categories);
        m.put("columnData", columnData);
        m.put("splineData", splineData);

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson report(){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        StringBuilder sql = new StringBuilder();
        sql.append("select Y.* From ( ");
        sql.append(" select * from ( ");
        sql.append("  select count(*) as report_count, to_char(c.JBRQ,'yyyy-mm') as date_month from CC_DAILYREPORT c group by to_char(c.JBRQ,'yyyy-mm') ");
        sql.append(" ) X  order by X.date_month desc ");
        sql.append(") Y where ROWNUM <= 6");

        List<Map<String, Object>> result = secondaryJdbc.queryForList(sql.toString());

        Collections.reverse(result);

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: result){
            categories.add(date.get("DATE_MONTH").toString());
        }

        //纵坐标
        List<Double> columnData = new ArrayList<>();
        List<Double> splineData = new ArrayList<>();
        for(Map<String, Object> obj: result){
            double orderCount = Double.parseDouble(obj.get("REPORT_COUNT").toString());
            columnData.add(orderCount);
            splineData.add(orderCount);
        }

        m.put("categories", categories);
        m.put("columnData", columnData);
        m.put("splineData", splineData);

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson sign() {
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        StringBuilder sql = new StringBuilder();
        sql.append("select Y.* From ( ");
        sql.append(" select * from ( ");
        sql.append("  select count(*) as sign_count, to_char(c.create_date,'yyyy-mm') as date_month from CC_SIGN c where c.sign_type=1 group by to_char(c.create_date,'yyyy-mm') ");
        sql.append(" ) X  order by X.date_month desc ");
        sql.append(") Y where ROWNUM <= 6");

        List<Map<String, Object>> result = secondaryJdbc.queryForList(sql.toString());

        Collections.reverse(result);

        //横坐标
        List<String> categories = new ArrayList<>();
        for (Map<String, Object> date : result) {
            categories.add(date.get("DATE_MONTH").toString());
        }

        //纵坐标
        List<Double> columnData = new ArrayList<>();
        List<Double> splineData = new ArrayList<>();
        for (Map<String, Object> obj : result) {
            double orderCount = Double.parseDouble(obj.get("SIGN_COUNT").toString());
            columnData.add(orderCount);
            splineData.add(orderCount);
        }

        m.put("categories", categories);
        m.put("columnData", columnData);
        m.put("splineData", splineData);

        j.setAttributes(m);

        return j;
    }

    @Override
    public String queryPmd(){
        String signSql = "select u.realname, s.address, to_char(s.create_date, 'yyyy-MM-dd HH24:MI') as DATE_STR, ROUND(TO_NUMBER(sysdate - s.create_date) * 24 * 60 * 60) from cc_sign s left join t_s_base_user u on u.id = s.user_id where ROUND(TO_NUMBER(sysdate - s.create_date) * 24 * 60 * 60) between 0 and " + SECONDS + " order by s.create_date desc";
        List<Map<String, Object>> signList = secondaryJdbc.queryForList(signSql.toString());

        String orderSendSql = "select s.work_code, s.send_opt_name from cc_work_card s where ROUND(TO_NUMBER(sysdate - s.send_time) * 24 * 60 * 60) between 0 and " + SECONDS + " and s.status = '1' order by s.send_time desc";
        List<Map<String, Object>> orderSendList = secondaryJdbc.queryForList(orderSendSql.toString());

        String orderReceiveSql = "select s.work_code, u.realname from cc_work_card s left join t_s_base_user u on s.worker_id = u.id where ROUND(TO_NUMBER(sysdate - s.receive_time) * 24 * 60 * 60) between 0 and " + SECONDS + " and s.status = '1' and s.receive_time is not null order by s.receive_time desc";
        List<Map<String, Object>> orderReceiveList = secondaryJdbc.queryForList(orderReceiveSql.toString());

        String orderCloseSql = "select s.work_code, s.close_user_name from cc_work_card s where ROUND(TO_NUMBER(sysdate - s.close_time) * 24 * 60 * 60) between 0 and " + SECONDS + " and s.status in (7,8,9) and s.close_time is not null order by s.close_time desc";
        List<Map<String, Object>> orderCloseList = secondaryJdbc.queryForList(orderCloseSql.toString());

        String result = "";

        if(signList.size() > 0){
            result += "有新的签到：";
        }
        for(Map m: signList){
            result += m.get("REALNAME") + "(" + m.get("ADDRESS") + ")，";
        }

        if(orderSendList.size() > 0){
            result += "有新发起的工单：";
        }
        for(Map m: orderSendList){
            result += m.get("work_code") + "(发起人：" + m.get("send_opt_name") + ")，";
        }

        if(orderReceiveList.size() > 0){
            result += "有新接单的工单：";
        }
        for(Map m: orderReceiveList){
            result += m.get("work_code") + "(接单人：" + m.get("realname") + ")，";
        }

        if(orderCloseList.size() > 0){
            result += "有新关单的工单：";
        }
        for(Map m: orderCloseList){
            result += m.get("work_code") + "(关单人：" + m.get("close_user_name") + ")，";
        }

        if(result.endsWith("，")){
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    private String getQuarterStr(int index){
        String result = "";
        if(index == 1){
            result = "一季度";
        }else if(index == 2){
            result = "二季度";
        }else if(index == 3){
            result = "三季度";
        }else if(index == 4){
            result = "四季度";
        }
        return result;
    }

    private String getWeekStr(int index){
        String result = "";
        if(index == 1){
            result = "第一周";
        }else if(index == 2){
            result = "第二周";
        }else if(index == 3){
            result = "第三周";
        }else if(index == 4){
            result = "第四周";
        }
        return result;
    }

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        System.out.println(c.getMaximum(Calendar.WEEK_OF_MONTH));
    }
}
